package org.locker.service.impl;

import org.locker.common.core.utils.MapstructUtils;
import org.locker.common.core.utils.StringUtils;
import org.locker.common.mybatis.core.page.TableDataInfo;
import org.locker.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.locker.domain.Repair;
import org.locker.domain.bo.RepairBo;
import org.locker.domain.vo.RepairVo;
import org.locker.mapper.RepairMapper;
import org.locker.service.IRepairService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 报修Service业务层处理
 *
 * @author winnie
 * @date 2023-12-11
 */
@RequiredArgsConstructor
@Service
public class RepairServiceImpl  extends ServiceImpl<RepairMapper, Repair>  implements IRepairService {

    private final RepairMapper baseMapper;

    /**
     * 查询报修
     */
    @Override
    public RepairVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询报修列表
     */
    @Override
    public TableDataInfo<RepairVo> queryPageList(RepairBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Repair> lqw = buildQueryWrapper(bo);
        Page<RepairVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询报修列表
     */
    @Override
    public List<RepairVo> queryList(RepairBo bo) {
        LambdaQueryWrapper<Repair> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Repair> buildQueryWrapper(RepairBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Repair> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), Repair::getType, bo.getType());
        lqw.eq(bo.getUserId() != null, Repair::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), Repair::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceNo()), Repair::getDeviceNo, bo.getDeviceNo());
        lqw.eq(bo.getDeviceGridId() != null, Repair::getDeviceGridId, bo.getDeviceGridId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Repair::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增报修
     */
    @Override
    public Boolean insertByBo(RepairBo bo) {
        Repair add = MapstructUtils.convert(bo, Repair.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改报修
     */
    @Override
    public Boolean updateByBo(RepairBo bo) {
        Repair update = MapstructUtils.convert(bo, Repair.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Repair entity){
        //做一些数据校验,如唯一约束
    }

    /**
     * 批量删除报修
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
